﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.BankCode;
using WaterRevenueMS.Domain.ViewModels.BasicSet.BankSet;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;


namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class BankSetController : AdminController
    {

        #region 属性
        [Import]
        public IBankSetService BankSetService { get; set; }

        [Import]
        public IBankCodeService BankCodeService { get; set; }
        #endregion

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new BankSetModel();
            #region 银行代码设置
            var parentModuleData = BankCodeService.BankCodeS.Where(t => t.IsDeleted == false)
            .Select(t => new BankSetModel
            {
                Id = t.Id,
                Yin_HMC = t.Yin_HMC
            });
            foreach (var item in parentModuleData)
            {
                model.Search.BankCodeItems.Add(new SelectListItem { Text = item.Yin_HMC, Value = item.Id.ToString() });
            }
            #endregion

            return View(model);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = BankSetService.BankSets.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = BankSetService.BankSets.Where(expr).Select(t => new BankSetModel
            {
                Id = t.Id,
                BankCode_Name = t.BankCode != null ? t.BankCode.Yin_HMC : "",
                Yin_HMC = t.Yin_HMC,
                Yin_HJC = t.Yin_HJC,
                Yin_HDZ = t.Yin_HDZ,
                Dian_H = t.Dian_H,
                Chuan_Z = t.Chuan_Z,
                Lian_XR = t.Lian_XR,
                OrderNo = t.OrderNo

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.BankCode_Name,
                            c.Yin_HMC,
                            c.Yin_HJC,
                            c.Yin_HDZ,
                            c.Dian_H,
                            c.Chuan_Z,
                            c.Lian_XR,
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new BankSetModel();
            InitBankCodeModel(model);
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(BankSetModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = BankSetService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new BankSetModel();
            var entity = BankSetService.BankSets.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new BankSetModel
                {
                    Id=entity.Id,
                    Yin_HDM_No=entity.Yin_HDM_No,
                    Yin_HDZ = entity.Yin_HDZ,
                    Yin_HJC = entity.Yin_HJC,
                    Yin_HMC = entity.Yin_HMC,
                    Chuan_Z = entity.Chuan_Z,
                    Dian_H =entity.Dian_H,
                    Lian_XR = entity.Lian_XR
                };
                InitBankCodeModel(model);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(BankSetModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = BankSetService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = BankSetService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<BankSet, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<BankSet> bulider = new DynamicLambda<BankSet>();
            Expression<Func<BankSet, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Yin_HMC"]))
            {
                var data = Request["Yin_HMC"].Trim();
                Expression<Func<BankSet, Boolean>> tmp = t => t.Yin_HMC.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yin_HJC"]) && Request["Yin_HJC"] != "0")
            {
                var data = Request["Yin_HJC"].Trim();
                Expression<Func<BankSet, Boolean>> tmp = t => t.Yin_HJC.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            if (!string.IsNullOrEmpty(Request["Yin_HDZ"]))
            {
                var data = Request["Yin_HDZ"].Trim();
                Expression<Func<BankSet, Boolean>> tmp = t => t.Yin_HDZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Dian_H"]) && Request["Dian_H"] != "0")
            {
                var data = Request["Dian_H"].Trim();
                Expression<Func<BankSet, Boolean>> tmp = t => t.Yin_HJC.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            if (!string.IsNullOrEmpty(Request["Chuan_Z"]))
            {
                var data = Request["Chuan_Z"].Trim();
                Expression<Func<BankSet, Boolean>> tmp = t => t.Chuan_Z.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Lian_XR"]) && Request["Lian_XR"] != "0")
            {
                var data = Request["Lian_XR"].Trim();
                Expression<Func<BankSet, Boolean>> tmp = t => t.Yin_HJC.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }


            if (!string.IsNullOrEmpty(Request["Yin_HDM_No"]) && Request["Yin_HDM_No"] != "0")
            {
                var data = Convert.ToInt32(Request["Yin_HDM_No"]);
                Expression<Func<BankSet, Boolean>> tmp = t => (t.Id == data) || (t.Yin_HDM_No == data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<BankSet, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

        private void InitBankCodeModel(BankSetModel model)
        {
            var parentModuleData = BankCodeService.BankCodeS.Where(t => t.IsDeleted == false)
            .Select(t => new BankSetModel
            {
                Id = t.Id,
                Yin_HMC = t.Yin_HMC
            });
            foreach (var item in parentModuleData)
            {
                model.BankCodeItems.Add(new SelectListItem { Text = item.Yin_HMC, Value = item.Id.ToString() });
            }
        }


    }
}